import { Component, OnInit } from '@angular/core';
import * as _ from 'lodash';
import { FormsModule, FormGroup, FormControl } from '@angular/forms';
import { environment } from '../../../../environments/environment';
import { AppPaginationComponent, PaginationModel } from "../../../widgets/pagination/pagination.component";
import { CustomerService } from "./customer.service";
import { DataService } from '../../../services/data.service';
import { Util } from 'app/common/util';
@Component({
  selector: 'app-customer',
  templateUrl: './customer.component.html',
  styleUrls: ['./customer.component.scss'],
  providers: [CustomerService]
})
export class CustomerComponent implements OnInit {
  // lodash
  public _: any = _;
  // 菜单数据
  public menuData: any;
  // 菜单按钮集合
  public menuResourceSet: any;
  // 客户信息分页post
  public getCustomerPost = {
    pageIndex: 1,
    pageSize: 10,
    sFullName: '',
    sFundCode: '',
    sPrincipal: ''
  };
  // 客户信息分页配置
  public customerListPageInfo: PaginationModel = {
    currentPageNum: 1,
    pageSize: 10,
    totalPages: 1,
    total: 0,
    pagesShow: 5,
    startRow: 0,
    endRow: 0,
    pageList: [5, 10, 25, 50, 100]
  };
  // 客户信息分页列表
  public customerList = [];
  // 客户信息列表是否全选
  public isCheckedAll = false;

  constructor(
    public customerService: CustomerService,
    public dataservice: DataService
  ) {

  }

  ngOnInit() {
    let that = this;
    this.list();
    // 权限处理
    this.dataservice.initTree.subscribe((data) => {
      let SessionMenuData = sessionStorage.getItem('menu');
      this.menuData = JSON.parse(SessionMenuData)
      this.menuResourceSet = [];
      _.forEach(this.menuData, (item, index) => {
        if (item.sName == '一对一专户') {
          _.forEach(item.children, (it, idx) => {
            if (it.sName == '客户信息') {
              that.menuResourceSet = it.resourceSet
              sessionStorage.setItem('btn-oto', that.menuResourceSet);
            }
          })
        }
      })
    })
    let SessionMenuData = sessionStorage.getItem('menu');
    this.menuData = JSON.parse(SessionMenuData)
    this.menuResourceSet = [];
    _.forEach(this.menuData, (item, index) => {
      if (item.sName == '一对一专户') {
        _.forEach(item.children, (it, idx) => {
          if (it.sName == '客户信息') {
            that.menuResourceSet = it.resourceSet
            sessionStorage.setItem('btn-oto', that.menuResourceSet);
          }
        })
      }
    })
  }

  // 分页列表
  list(pageNum?, pageSize?) {
    let that = this;
    this.isCheckedAll = false;
    this.getCustomerPost.sFullName = _.trim(this.getCustomerPost.sFullName);
    this.getCustomerPost.sFundCode = _.trim(this.getCustomerPost.sFundCode);
    this.getCustomerPost.sPrincipal = _.trim(this.getCustomerPost.sPrincipal);
    this.getCustomerPost.pageIndex = pageNum ? pageNum : this.customerListPageInfo.currentPageNum;
    this.getCustomerPost.pageSize = pageSize ? pageSize : this.customerListPageInfo.pageSize;
    this.customerService.list(this.getCustomerPost).subscribe(data => {
      if (data) {
        that.customerList = data.list;
        that.customerListPageInfo.currentPageNum = data.pageNum;
        that.customerListPageInfo.totalPages = data.pages;
        that.customerListPageInfo.total = data.total;
        that.customerListPageInfo.startRow = data.startRow;
        that.customerListPageInfo.endRow = data.endRow;
      }
      // // 给表格列支持列拖动效果
      // setTimeout(() => {
      //   Util.colResizable("#fixed-table");
      // }, 100);

      // 给表格列支持列拖动和固定表头效果
      Util.colResizable("#customerList");
    });
  }
  // 个数分页
  customerpageSizeChange(pageSize: number) {
    if (pageSize !== this.customerListPageInfo.pageSize) {
      this.customerListPageInfo.pageSize = pageSize;
      this.list();
    }
  }
  // 页码分页
  customerPageNavigation(currentPageNum: number) {
    this.customerListPageInfo.currentPageNum = currentPageNum;
    this.list(this.customerListPageInfo.currentPageNum, this.customerListPageInfo.pageSize);
  }
  // 列表全选
  checkedAll() {
    if (this.isCheckedAll) {
      _.forEach(this.customerList, item => {
        item.isChecked = true;
      });
    } else {
      _.forEach(this.customerList, item => {
        item.isChecked = false;
      });
    }
  }
  // 列表是否全选
  ctrlCheckAll() {
    let that = this;
    let checkList = [];
    _.forEach(this.customerList, item => {
      if (item.isChecked == true) {
        checkList.push(item);
      }
    })
    if (checkList.length == this.customerList.length) {
      this.isCheckedAll = true;
    } else {
      this.isCheckedAll = false;
    }
  }
  // 导出
  exportExcel() {
    let that = this;
    let checkedItem = [];
    _.forEach(this.customerList, item => {
      if (item.isChecked) {
        checkedItem.push(item.nCustomerNo);
      }
    })
    let converItem: any = checkedItem.join(',');
    let getBody = {
      "nCustomerNos": converItem,
      "sFullName": this.getCustomerPost.sFullName,
      "sFundCode": this.getCustomerPost.sFundCode,
      "sPrincipal": this.getCustomerPost.sPrincipal
    }
    const iframe = window['$']("<iframe id='downloadiframe'>");
    iframe.attr('style', 'display:none');
    //  转码，兼容ie
    getBody.sFullName = encodeURI(getBody.sFullName);
    getBody.sFundCode = encodeURI(getBody.sFundCode);
    getBody.sPrincipal = encodeURI(getBody.sPrincipal);
    iframe.attr('src', environment.server + '/pms/v1/customer/export?token=' + sessionStorage.getItem('token') + '&nCustomerNos=' + getBody.nCustomerNos + '&sFullName=' + getBody.sFullName + '&sFundCode=' + getBody.sFundCode + '&sPrincipal=' + getBody.sPrincipal);
    window['$']('body').append(iframe);
    setTimeout("$('#downloadiframe').remove()", 60000);
  }
  // 点击表格头排序
  orderData(colName) {
    let listDataAsc = _.orderBy(this.customerList, [colName], ["asc"]);
    if (JSON.stringify(this.customerList) == JSON.stringify(listDataAsc)) {
      listDataAsc = _.orderBy(this.customerList, [colName], ["desc"]);
    }
    this.customerList = listDataAsc;
  }
}
